/**********************************************************************************************
This code produces Table B1 and Table B2 to understand the representativeness of
ASMB defaults.
**********************************************************************************************/
clear all


* Create local list of LTV statistics 
local ltv_vars 
foreach var in LTV_FHFA_st LTV_FHFA_county LTV_FHFA_tract LTV_FHFA_timeofsurvey cltv_all {
foreach pp in 10 25 50 75 90 {
	local ltv_vars `ltv_vars' (p`pp') p`pp'_`var' = `var' 
}
}

* Create local list of other variables to look at
local other_vars everdelqany  conventional_loan age_1 arm borrowers dti ethnic_1 male med_income_hmda_2019 mortgage_age open_month white black score_orig_1 term income loan_amt value interest ltv cltv_all pct_minority_tract urban LTV_FHFA_st LTV_FHFA_county LTV_FHFA_tract 


********************************************************************************
* Create summary statistics for defaulters from the NMDB
********************************************************************************

use "$DATA_OUT/NMDB_sample_for_ASMB_comparison", clear

gen sample     = "current" if defaulter==0 & new_loan==0
replace sample = "defaulter" if in_default_sample==1 & new_loan==0
replace sample = "current_new" if defaulter==0 & new_loan==1 
replace sample = "defaulter_new" if in_default_sample==1 & new_loan==1
tab sample

* Report in the text the % of NMDB defaulters that are "fast" defaulters
tab sample if regexm(sample,"defaulter")

gen MtM_value     	    = MtM_value_FHFA_county
replace MtM_value 	    = MtM_value_BK_cnty_coarse if mi(MtM_value)
gen percent_change_in_value = 100*(MtM_value / value)
gen tract_county_ratio 	    = 100*(MtM_value_FHFA_tract / MtM_value)
gen LTV_change 		    = LTV_FHFA_county - cltv_all

do "$DOFILES/9999_prep_variables_for_NMDB_ASMB_comparison.do"


collapse `ltv_vars' (mean) `other_vars' , by(sample)
order sample everdelq
save "$DATA_OUT/processing/summary_statistics_NMDB", replace


********************************************************************************
* Create table B1 comparing "fast" defaulters to other defaulters
********************************************************************************

use "$DATA_OUT/processing/summary_statistics_NMDB", clear

foreach var in p10_LTV_FHFA_tract p25_LTV_FHFA_tract p50_LTV_FHFA_tract p75_LTV_FHFA_tract p90_LTV_FHFA_tract p10_cltv_all p25_cltv_all p50_cltv_all p75_cltv_all p90_cltv_all everdelqany {
	replace `var' = round(`var',.1)

}

order  sample everdelqany *LTV_FHFA_tract *cltv_all
browse sample everdelqany *LTV_FHFA_tract *cltv_all
keep if regexm(sample,"defaulter")






********************************************************************************
* Now work on Table B2.  Begin by creating summary statistics for ASMB 
* recipients, whether or not they respondend
********************************************************************************
* Bring in main data
use "$DATA_OUT/NMDB_ASMB_recipients", clear 


* Define some variables etc we need
do "$DOFILES/9999_prep_variables_for_NMDB_ASMB_comparison.do"


* Separate the data into two groups we want statistics for, (1) borrowers in the current stratum
* and (2) defaulters
gen strata = substr(survey_asmb,2,1)
gen sample 	= "ASMB_recipients_current" if strata=="C"
replace sample 	= "ASMB_recipients_defaulter" if strata!="C" & in_default_sample==1
drop if mi(sample)

* Collapse to get the statistics
collapse `ltv_vars' (mean) `other_vars', by(sample)

save "$DATA_OUT/processing/summary_statistics_ASMB_recipients", replace




********************************************************************************
* Now get summary statistics for ASMB respondents
********************************************************************************

* Bring in all the ASMB data
use "$DATA_OUT/NMDB_ASMB_respondents", clear 

* Create some useful variables we'll look at (% black, % hispanic, etc.)
do "$DOFILES/9999_prep_variables_for_NMDB_ASMB_comparison.do"

* Set weights
svyset [pweight = analysis_weight]

* Statistics for unweighted ASMB 
preserve
gen sample = "ASMB unweighted current" if heavywgt==1
replace sample = "ASMB unweighted defaulter " if in_default_sample==1
drop if mi(sample)
collapse `ltv_vars' (mean)  `other_vars'  , by(sample)
save "$DATA_OUT/processing/summary_statistics_ASMB_respondents_unweighted", replace
restore

* Statistics for weighted ASMB
gen sample = "ASMB weighted current" if heavywgt==1
replace sample = "ASMB weighted defaulter" if in_default_sample==1
drop if mi(sample)
collapse `ltv_vars' (mean) `other_vars'  [pweight = analysis_weight], by(sample)

********************************************************************************
* Finally, bring the statistics for the NMBD, ASMB recipients, and ASMB respondents all together
********************************************************************************


append using "$DATA_OUT/processing/summary_statistics_ASMB_respondents_unweighted"
append using "$DATA_OUT/processing/summary_statistics_ASMB_recipients"
append using  "$DATA_OUT/processing/summary_statistics_NMDB"
order sample everdelq

save "$DATA_OUT/processing/summary_stats_in_ASMB_and_NMDB", replace



use  "$DATA_OUT/processing/summary_stats_in_ASMB_and_NMDB", clear
drop if mi(sample)
drop if sample=="new_loan"

preserve
xpose, clear varname
gen merge_n = _n-1
keep merge_n _varname
save "$DATA_OUT/processing/summary_stats_ASMB_NMDB_varnames", replace
restore


replace sample = subinstr(sample," ","_",4)
sxpose, clear firstnames force

gen merge_n = _n
merge 1:1 merge_n using "$DATA_OUT/processing/summary_stats_ASMB_NMDB_varnames", keep(master match) nogen


foreach var in ASMB_weighted_current ASMB_weighted_defaulter ASMB_unweighted_current ASMB_unweighted_defaulter_ ASMB_recipients_current ASMB_recipients_defaulter current defaulter {
	destring(`var'), replace
	replace `var' = 100 * `var' if _varname=="io_flag"
	replace `var' = round(`var',.1)
}


drop merge_n
order _varname ASMB_unweighted_current ASMB_weighted_current ASMB_recipients_current current ASMB_unweighted_defaulter_ ASMB_weighted_defaulter ASMB_recipients_defaulter defaulter

rename current NMDB_current
rename defaulter NMDB_defaulter
